<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/common_head :: commonHeader('首页')">
</head>
<body>

token:<input type="text" id="token"/><input type="button" value="连接" onclick="connect()"/><br>
msg:<input type="text" id="msg"/><input type="button" value="发送" onclick="sendMsg()"/>

<script type="text/javascript">

    var ws = null;
    var host = window.location.host;

    function connect() {

        if ($("#token").val() != null && $("#token").val() != "") {
            if ('WebSocket' in window) {
                ws = new WebSocket("ws://" + host + "/websocket/" + $("#token").val());
            } else if ('MozWebSocket' in window) {
                ws = new WebSocket("ws://" + host + "/websocket/" + $("#token").val());

            } else {
                alert("该浏览器不支持websocket");
            }

            ws.onmessage = function (evt) {
                alert(evt.data);
            };

            ws.onclose = function (evt) {
                alert("连接中断");
            };

            ws.onopen = function (evt) {
                alert("连接成功");
            };
        } else {
            alert("请输入token");
        }

    }

    function sendMsg() {
        ws.send($("#msg").val());
    }

</script>


</body>
</html>